import pyautogui
from pynput import keyboard
import threading

# 打印屏幕尺寸
screen_width, screen_height = pyautogui.size()
print(f"屏幕尺寸: {screen_width}x{screen_height}")

# 标记特定按键是否被按下
key_pressed = False

# 创建一个事件对象
key_event = threading.Event()

def on_press(key):
    global key_pressed
    try:
        if key.char == 'g':  # 监听 g 键
            key_pressed = True
            key_event.set()
            try:
                pixel_color = pyautogui.pixel(2379, 1179)
                print(f"像素颜色 (2416, 1275): {pixel_color}")
            except Exception as e:
                print(f"获取像素颜色时出错: {e}")
            print("g 键被按下，继续执行后续代码...")
    except AttributeError:
        pass  # 忽略非字符键

def wait_for_key():
    print("启动键盘监听器...")
    with keyboard.Listener(on_press=on_press) as listener:
        listener.join()

# 启动键盘监听线程
keyboard_thread = threading.Thread(target=wait_for_key, daemon=True)
keyboard_thread.start()

# 等待特定按键按下
key_event.wait()
print("按键已按下，继续执行后续代码...")
pixel_color = pyautogui.pixel(2416, 1275)
print(f"像素颜色 (2416, 1275): {pixel_color}")